package cn.qidisoft.edu.hzjt.service.xb.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.qidisoft.core.common.code.QdfRespCode;
import cn.qidisoft.core.common.exception.dao.DBException;
import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.common.pagination.Page;
import cn.qidisoft.core.common.pagination.PaginationBean;
import cn.qidisoft.edu.hzjt.dao.xb.WindUpStudyMapper;
import cn.qidisoft.edu.hzjt.model.xb.WindUpStudyEn;
import cn.qidisoft.edu.hzjt.service.xb.IWindUpStudyService;
import cn.qidisoft.edu.hzjt.utils.LogType;

@Service
public class WindUpStudyServiceImpl implements IWindUpStudyService {

    protected static final LoggerAdapter LOGGER = LoggerAdapterFactory
        .getLogger(LogType.HZJT_LOGGER.val);

    @Autowired
    private WindUpStudyMapper            mapper;

    @Override
    public List<WindUpStudyEn> queryList(WindUpStudyEn model) {
        return mapper.queryList(model);
    }

    @Override
    public PaginationBean<WindUpStudyEn> queryPageList(WindUpStudyEn model, Page page) {
        try {
            int totalRecords = mapper.queryPageListcount(model);

            PaginationBean<WindUpStudyEn> pageHolder = new PaginationBean<>(page, totalRecords);
            List<WindUpStudyEn> resultList = mapper.queryPageList(model,
                new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                    pageHolder.getPageSize()));
            pageHolder.setPageList(resultList);
            return pageHolder;
        } catch (Exception e) {
            LOGGER.error("queryPageList [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(model, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    @Override
    public WindUpStudyEn queryByPK(WindUpStudyEn model) {
        return mapper.queryByPK(model);
    }

    @Override
    public int insert(WindUpStudyEn model) {
        return mapper.insert(model);
    }

    @Override
    public int update(WindUpStudyEn model) {
        mapper.update(model);
        return 0;
    }

    @Override
    public int delete(WindUpStudyEn model) {
        mapper.delete(model);
        return 0;
    }

    @Override
    public List<Map<String, Object>> queryAllYearList() {
        return mapper.queryAllYearList();
    }

    @Override
    public Map<String, Object> queryUserDeptAndOrg(int empId) {
        return mapper.queryUserDeptAndOrg(empId);
    }

    @Override
    public PaginationBean<Map<String, Object>> queryUserAllAuditData(Map<String, Object> map,
                                                                     Page page) {
        try {
            int totalRecords = mapper.queryUserAllAuditDataCount(map);

            PaginationBean<Map<String, Object>> pageHolder = new PaginationBean<>(page,
                totalRecords);
            List<Map<String, Object>> resultList = mapper.queryUserAllAuditData(map,
                new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                    pageHolder.getPageSize()));
            pageHolder.setPageList(resultList);
            return pageHolder;
        } catch (Exception e) {
            LOGGER.error("queryPageList [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(map, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    @Override
    public List<Map<String, Object>> queryAllDeptList() {
        return mapper.queryAllDeptList();
    }

    @Override
    public List<Map<String, Object>> queryAllOrgList() {
        return mapper.queryAllOrgList();
    }

    @Override
    public List<Map<String, Object>> exportDataList(Map<String, Object> map, Page page) {
        try {
            PaginationBean<Map<String, Object>> pageHolder = new PaginationBean<>(page, 999999);
            List<Map<String, Object>> resultList = mapper.queryUserAllAuditData(map,
                new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                    pageHolder.getPageSize()));

            return resultList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /** 
    * @see cn.qidisoft.edu.hzjt.service.xb.IWindUpStudyService#updateStates(cn.qidisoft.edu.hzjt.model.xb.WindUpStudyEn)
    */
    @Override
    public int updateStates(WindUpStudyEn data) {
        try {
            return mapper.updateStates(data);
        } catch (Exception e) {
            LOGGER.error("updateStates [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }
}
